import Vue from 'vue'
import VueRouter from 'vue-router'
import About from '../views/About'
import ItemCreate from '../views/ItemCreate';
import ItemUpdate from '@/views/ItemUpdate';
import ItemList from '@/views/ItemList';
import Order from '@/views/Order';
import Login from '@/views/Login';
import Index from '@/views/Index';
import Manager from '@/views/Manager';
import Statistic from '@/views/Statistic';
import OrderDetail from '@/views/OrderDetail';
import Cashier from '@/views/Cashier';


Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'Login',
    component: Login,
  },
  {
    path: '/',
    name: '',
    component: Index,
    redirect: {name: 'Cashier'},
    children: [
      {
        path: '/cashier',
        name: 'Cashier',
        component: Cashier,
      },
      {
        path: '/about',
        name: 'About',
        component: About,
      },
      {
        path: '/statistic',
        name: 'Statistic',
        component: Statistic,
      },
      {
        path: '/manager',
        name: 'Manager',
        component: Manager,
        redirect: '/item/list',
        children: [
          {
            path: '/item/list',
            name: 'ItemList',
            component: ItemList,
          },
          {
            path: '/item/create',
            name: 'ItemCreate',
            component: ItemCreate,
          },
          {
            path: '/item/update/:id',
            name: 'ItemUpdate',
            component: ItemUpdate,
          },
          {
            path: '/order/list',
            name: 'Order',
            component: Order,
          },
          {
            path: '/order/list/:id',
            name: 'OrderDetail',
            component: OrderDetail,
          },
        ],
      },
    ],
  },

]


const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes,
})

router.beforeEach(async (to, from, next) => {
  const TOKEN = localStorage.getItem('token');
  if (!TOKEN && to.name !== 'Login') {
    next({name: 'Login', replace: true})
    return
  }
  next()
})

export default router
